package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.business.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 攻略分类Service业务层处理
 * 
 * @author zty
 * @date 2025-09-16
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper,StrategyCatalog> implements IStrategyCatalogService {
    @Resource
    private StrategyMapper strategyMapper;
    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCatalog> queryCatalogList(Long destId) {
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId).list();
        for (StrategyCatalog catalog : list) {
            //条件构造器
            LambdaQueryWrapper<Strategy> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Strategy::getCatalogId, catalog.getId());
            List<Strategy> strategies = strategyMapper.selectList(wrapper);
            catalog.setStrategies(strategies);
        }
        return list;
    }

    /*SELECT dest_name, GROUP_CONCAT(id) ids, GROUP_CONCAT(name) names
from ta_strategy_catalog
GROUP BY dest_name*/
    @Override
    public List<CatalogVO> queryGroupList() {
        QueryWrapper<StrategyCatalog> queryWrapper = new QueryWrapper<>();
        queryWrapper.groupBy("dest_name");
        queryWrapper.select("dest_name, GROUP_CONCAT(id) ids, GROUP_CONCAT(name) names");
        List<Map<String, Object> >maps = baseMapper.selectMaps(queryWrapper);
        List<CatalogVO> catalogVOS = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String destName = map.get("dest_name").toString();
            String ids = map.get("ids").toString();
            String names = map.get("names").toString();
            String[] idArr = ids.split(",");
            String[] nameArr = names.split(",");
            List<Map<String, Object> >catalogs = new ArrayList<>();
            for (int i = 0; i < idArr.length; i++){
                Long id = Long.valueOf(idArr[i]);
                String name = nameArr[i];
               Map<String, Object> m=new HashMap<>();
               m.put("id", id);
               m.put("name", name);
               catalogs.add( m);
            }
        catalogVOS.add(new CatalogVO(destName, catalogs));
        }

return catalogVOS;
    }
}
